package com.sulent.frame.file.utils;

import com.sulent.frame.file.dto.DirTree;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Item;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @ClassName MinioClientUtils
 * @Description: TODO
 * @Author Submerge.
 * @Since 2024/2/4 11:55
 * @Version 1.0
 */
public class MinioClientUtils {


    public static void main(String[] args) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String endpoint= "http://miniosvc.dgdtest.com.cn:30080/";
        String accessKey="YN8ZFLq8jE0p5XTsfxFN";
        String secretKey="BOgi2LWcIk4xWqZ8uYRkd3CX2RRzA8n4NZDohoPj";
        String bucketName="datacenter";
        MinioClient minioClient = MinioClient
                .builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();

        boolean sub = false;
        try {
            sub = MinioUtils.objectExist(minioClient, bucketName, "sub");
        } catch (MinioException e) {
            e.printStackTrace();
        }
        if (!sub){
            System.out.println("文件不存在！");
        }
        minioClient=null;

/*        List<DirTree> dirTreeByPath = MinioUtils.getDirTreeByPath(minioClient, "data/", 2);
        System.out.println(dirTreeByPath.toString());
        System.exit(0);*/

/*        String path = "data/uploadfile/";
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(path).build());
        for (Result<Item> next : results) {
            Item item = next.get();
            System.out.print(item.objectName());
            if (item.isDir()){
                System.out.println("--这是目录");
            }else {
                System.out.println("--这是文件");
            }
        }
        minioClient.traceOff();
        System.exit(0);*/
/*

        FileInputStream fileInputStream = new FileInputStream(new File("D:\\data\\merge\\产品推广.pptx"));
        ObjectWriteResponse objectWriteResponse = minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object("minio-test/2.pptx").stream(fileInputStream, fileInputStream.available(), -1).build());
*/



    }
}
